<?php

require_once 'Zend/Json/Decoder.php';
require_once 'Zend/Http/Client.php';
require_once 'Zend/Http/Client/Exception.php';
require_once 'HttpClient.php';
require_once 'HttpClientAuth.php';

/**
 * Description of VirtualMachine
 *
 * @author markos
 */
class CloudVirtualMachine {

    protected $_zoneId;
    protected $_logger;

    public function __construct($log) {
        $this->_logger = $log;
    }

    public function listVM($vmId = null) {
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug(" [ zoneid ] {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "listVirtualMachines";
        $url["listall"] = true;
//        $url["projectid"] = "80e87c53-4d90-4ae7-96f9-371109c33800";
        if (!is_null($vmId))
            $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new HttpClient($this->_logger);
        try {
            $response = $v->call($url);
            $this->_logger->debug(print_r($response, true));
        } catch (Exception $e) {
            $this->_logger->err($e->getTraceAsString());
            throw $e;
        }
        return Zend_Json_Decoder::decode($response);
    }

    public function apagarVM($vmId) {
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "stopVirtualMachine";
        $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new HttpClient($this->_logger);
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $this->_logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    public function encenderVM($vmId) {
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "startVirtualMachine";
        $url["id"] = $vmId;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $this->_logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    //put your code here
    public function createVM($serviceOffering, $diskOffering, $name, $template, $projectId) {
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "deployVirtualMachine";
        $url["serviceofferingid"] = $serviceOffering;
        $url["templateid"] = $template;
        $url["diskofferingid"] = $diskOffering;
        $url["displayname"] = $name;
        $url["projectid"] = $projectId;
        $url["name"] = $name;
        $url["zoneid"] = $this->_zoneId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
        } catch (Zend_Http_Client_Exception $e) {
            $this->_logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

    /*
     * 
     */

    public function deleteVM($vmId) {
        $config = new Zend_Config_Ini(CONFIGURATION_FILE, APPLICATION_ENV);
        $this->_zoneId = $config->get("zoneid");
        $this->_logger->debug("[zoneid] = {$this->_zoneId}");
        $url = array();
        $url["response"] = "json";
        $url["command"] = "destroyVirtualMachine";
        $url["id"] = $vmId;
        $v = new HttpClient();
        try {
            $response = $v->call($url);
            $this->_logger->debug("DeleteVM:" . print_r($response, TRUE));
        } catch (Zend_Http_Client_Exception $e) {
            $this->_logger->err($e->getMessage());
            $this->_logger->err($e->getTraceAsString());
            throw $e;
        }
        return ($response);
    }

}

?>
